﻿public class NetHandler_Like : GameServerMessageHandler
{
    public const string Key = "Like";

    public static NetHandler_Like NewHandler()
    {
        return new NetHandler_Like();
    }

    protected override ISerialize NewData() => new FromToUserRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        FromToUserRequestData requestData = _data as FromToUserRequestData;

        GameDebug.Log($"{client.userID} 用户{requestData.fromUserId} like:{requestData?.toUserId} ");

        UserServer user = Game.instance.GetUser(client.userID);

        ResponseData response = new ResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                if(requestData.toUserId > 0)
                    user.data.AddLike(requestData.toUserId);

                long fromUserId = requestData.fromUserId;

                long opponent = requestData.toUserId;
                if (opponent > 0)
                {
                    List<long> oppoBeLikes = new List<long>();
                    UserServer opponentUser = Game.instance.GetUser(opponent);
                    if (opponentUser != null)
                    {
                        opponentUser.data.AddBeLike(requestData.fromUserId);

                        GameDebug.Log($"给{opponent} 发送申请结果事件");
                        NetEvent_Like netEvent_Like = new NetEvent_Like();
                        netEvent_Like.likeData = new LikeData();
                        netEvent_Like.likeData.likeFromUserId = fromUserId;
                        netEvent_Like.SendToRemote(opponentUser.client);

                        oppoBeLikes = opponentUser.data.belikes;
                        users.UpdateBeLikes(opponent, oppoBeLikes);
                    }
                    else
                    {
                        users oppouserTable = UserServer.FindInUserTable(opponent);
                        if (oppouserTable != null)
                        {
                            oppoBeLikes = users.String2LongList(oppouserTable.belikes);
                            if (!oppoBeLikes.Contains(fromUserId))
                                oppoBeLikes.Add(fromUserId);
                            oppoBeLikes.Remove(0);
                            users.UpdateBeLikes(opponent, oppoBeLikes);
                        }
                    }
                }

                int ret1 = users.UpdateLikes(user.data.id,user.data.likes);

                if (ret1 >= 0)
                    response.code = 0;
                else
                    response.code = -1;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
